package com.example.jpatest.webapi;

import com.example.jpatest.entity.Student;
import com.example.jpatest.service.StudentService;
import com.example.jpatest.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {
    @Autowired
    private StudentService studentService;

    /**
     * 返回所有学生信息
     *
     * @return
     */
    @GetMapping("/list")
    public List<Student> getAll() {

        List<Student> students = studentService.findAll();
        return students;
    }

    /**
     * 根据姓名模糊查询
     *
     * @param name
     * @return
     */
    @GetMapping("/list/{name}")
    public List<Student> findByName(@PathVariable String name) {
        System.out.println(name);
        List<Student> stus = studentService.findByName(name);
        return stus;
    }

    /**
     * 新增学生信息
     *
     * @param student
     */
    @PostMapping("/insert")
    public void insert(Student student) {
        studentService.insert(student);
    }

    /**
     * 分页查询，带有条件
     *
     * @param page
     * @param size
     * @param name
     * @return
     */
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                               @RequestParam(value = "size", defaultValue = "5") Integer size,
                               @RequestParam(value = "name", defaultValue = "") String name,
                               @RequestParam(value = "no", defaultValue = "") String no) {
        //按id排序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Page<Student> studentPage = null;
        if (!StringUtils.isEmpty(name)) {
            Student student = new Student();
            student.setName(name);
            Pageable pageable = PageRequest.of(page, size, sort);
            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Student> example = Example.of(student, matcher);
            studentPage = studentService.findAll(example, pageable);

        } else if  (!StringUtils.isEmpty(no)) {
            Student student = new Student();
            student.setSno(no);
            Pageable pageable = PageRequest.of(page, size, sort);
            Example<Student> example = Example.of(student);
            studentPage = studentService.findAll(example, pageable);

        } else {
            Pageable pageable = PageRequest.of(page, size, sort);
            studentPage = studentService.findAll(pageable);
        }
        PageUtils pageUtils = new PageUtils(studentPage.getContent(), studentPage.getTotalElements());
        return pageUtils;
    }

    /**
     * 更新学生信息
     *
     * @param student
     */
    @PutMapping("/update")
    public void update(Student student) {
        //读取旧的数据
        Student oldStu = studentService.findById(student.getId());
        if (StringUtils.isEmpty(student.getPassword())) {
            // 没有新密码就是旧的密码
            student.setPassword(oldStu.getPassword());
        }
        studentService.update(student);
    }

    /**
     * 根据id删除学生信息
     *
     * @param id
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {
        studentService.delete(id);
    }

    /**
     * 根据id查询学生信息
     *
     * @param id
     * @return
     */

    @GetMapping("/get/{id}")
    public Student getById(@PathVariable Integer id) {
        Student stu = studentService.findById(id);
        stu.setPassword(" ");
        return stu;
    }
}
